<?php
namespace app\back\controller;
use app\common\controller\AdminBase;
use think\Loader;
use think\Request;
use think\Url;
use think\Controller;
class Check extends AdminBase {
    /**
     * 菜单列表
     * @author Zcc<2351976426@qq.com>
     * @dateTime 2016-12
     * @return [type] [description]
     */
    public function index() {
    $request = Request::instance();
        if ($request->isPost()) {
            $params = $request->param();
            //dump($params);exit;
            $starttime=strtotime($params['start_time']);
            $endtime=strtotime($params['end_time']);
            $ruleModel = Loader::model('Commit');
        if (!empty($params['zid'])&&empty($params['end_time'])&&empty($params['start_time'])) {
            $lists = $ruleModel->where(array('zid'=>$params['zid']))->paginate(5); 
             //dump($lists);exit;
        }elseif(empty($params['zid'])&&!empty($params['end_time'])&&!empty($params['start_time'])){
           $lists = $ruleModel->where('create_time','between time',[$starttime,$endtime])->paginate(5);
        }
        if(!empty($params['zid'])&&!empty($params['end_time'])&&!empty($params['start_time'])){
           $lists = $ruleModel->where(array('zid'=>$params['zid']))->where('create_time','between time',[$starttime,$endtime])->paginate(5);
        }
        
          } else{
            $ruleModel = Loader::model('Commit');
            $lists = $ruleModel::paginate(5);
          } 
       
            $this->assign('lists', $lists);
            $this->assign('pages', $lists->render());
            $ruleModel = Loader::model('User');
            $ruleRows = $ruleModel::select();;
            $this->assign('ruleRows', $ruleRows);
            return $this->fetch();
    }


    /**
     * 编辑
     * @author Zcc<2351976426@qq.com>
     * @dateTime 2016-12
     * @param  string $value [description]
     * @return [type] [description]
     */
    public function edit($id) {
        $ruleModel = Loader::model('Commit');
        $ruleRow = $ruleModel::get($id);
        if ($ruleRow === false) {
            $this->error('没有找到对应的数据');
        }

        $request = Request::instance();
        if ($request->isPost()) {
            $params = $request->param();
            //dump($params);exit();
            $time=$request->param('update_time');
            if(!empty($time)){
              $params['update_time']= strtotime($time);
            }
            $status=$request->param('status');
            
            $params['status']= $status;
            $params['id'] = $id;

            if (loader::validate('Check')->scene('edit')->check($params) === false) {
                return $this->error(loader::validate('Check')->getError());
            }
            if (Loader::model('Commit')->save($params, ['id' => $id]) === false) {
                return $this->error(loader::model('Commit')->getError());
            }
              Loader::model('SystemLog')->record("审核成功,ID:[{$id}]");
            return $this->success('审核成功', Url::build('back/Check/index'));
        }

        $ruleModel = Loader::model('User');
        $ruleRows = $ruleModel::select();;
        $this->assign('ruleRows', $ruleRows);
        $this->assign('ruleRow', $ruleRow);
        return $this->fetch();
    }
    /**
     * 删除
     * @author Zcc<2351976426@qq.com>
     * @dateTime 2016-12
     * @param  [type] $id [description]
     * @return [type] [description]
     */
    public function destroy() {

$request = Request::instance();
    if ($request->isPost()) {
        $id = $request->param('id');
        $RuleModel = Loader::model('Commit');
        if ($RuleModel->deleteRule($id) === false) {
            return $this->error($RuleModel->getError());
        }
          Loader::model('SystemLog')->record("删除申请列表,ID:[{$id}]");
        //return $this->success('菜单列表删除成功', Url::build('back/rule/index'));
          return ['msg'=>'申请列表删除成功','status'=>'1'];
    }
    }
}